{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "# 集合\n",
    "集合是由不重复元素组成的无序容器。\n",
    "基本用法包括成员检测、消除重复元素。\n",
    "集合支持合集、交集、差集、对称差分等数学运算。\n",
    "创建集合用花括号或set()函数。创建空集合只能用set()，不能用{}，{}创建的是空字典。"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'apple', 'pear', 'banana', 'orange'}\n",
      "4\n",
      "True\n",
      "False\n",
      "{'a', 'b', 'c', 'd', 'r'}\n",
      "{'a', 'l', 'c', 'm', 'z'}\n",
      "{'d', 'b', 'r'}\n",
      "{'a', 'l', 'b', 'c', 'd', 'm', 'z', 'r'}\n",
      "{'c', 'a'}\n",
      "{'d', 'l', 'm', 'z', 'b', 'r'}\n"
     ]
    }
   ],
   "source": [
    "# 使用{}创建集合\n",
    "basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}\n",
    "print(basket)\n",
    "print(len(basket))\n",
    "print('orange' in basket)\n",
    "print('grape' in basket)\n",
    "# 使用set()创建集合\n",
    "a = set('abracadabra')\n",
    "b = set('alacazam')\n",
    "print(a)\n",
    "print(b)\n",
    "# 差集\n",
    "print(a - b)\n",
    "# 合集\n",
    "print(a | b)\n",
    "# 交集\n",
    "print(a & b)\n",
    "# 异或\n",
    "print(a ^ b)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'d', 'r'}\n"
     ]
    }
   ],
   "source": [
    "# 集合也支持推导式\n",
    "a = {x for x in 'abracadabra' if x not in 'abc'}\n",
    "print(a)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}